package com.gxa.controller;

import com.gxa.common.Constants;
import com.gxa.common.PaginationResult;
import com.gxa.common.ResponseData;
import com.gxa.pojo.Employee;
import com.gxa.service.EmployeeBaseService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: tx
 * Date: 2020/10/27 10:00
 * Description:员工基础设置
 * Version: V1.0
 */
//@Controller
@RestController
@Api(value = "增加员工",tags = "员工基础设置")
@RequestMapping(value = "employee")
public class EmployeeBaseController {

    @Autowired
    private EmployeeBaseService employeeBaseService;
    @Autowired
    private ResponseData responseData;

    @PostMapping("/addEmployeeBase")
    @ApiOperation(value = "添加员工信息接口",notes = "根据提交的信息添加员工",httpMethod = "post",response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200,message = "添加成功"),
            @ApiResponse(code = 400,message = "找不到页面"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @ResponseBody
    public ResponseData addEmployeeBase(@RequestBody @ApiParam(name = "employee",value = "员工信息",required = true) Employee employee){
        System.out.println("添加员工信息接口"+employee);
        int result = employeeBaseService.addEmployeeBase(employee);
        if(result > 0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("添加成功");
            responseData.setData(null);
        }
        return responseData;
    }

    @PostMapping("/delEmployeeBase")
    @ApiOperation(value = "删除员工信息接口", notes = "根据信息删除员工信息接口", httpMethod = "post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 400, message = "找不到页面"),
            @ApiResponse(code = 500, message = "删除失败")
    })
    @ResponseBody
    public ResponseData delEmployeeBase(@RequestBody @ApiParam(name = "employeeId",value = "员工id",required = true) Employee employee){
        int result = employeeBaseService.delEmployeeBase(employee.getEmployeeId());
        if(result > 0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("删除成功");
            responseData.setData(null);
        }else{
            responseData.setCode(Constants.NOTFOUND);
            responseData.setMsg("该员工为在职状态，不能删除");
            responseData.setData(null);
        }
        return responseData;
    }


    @PostMapping("/updateEmployeeBase")
    @ApiOperation(value = "更新员工信息接口", notes = "根据信息更新员工信息接口", httpMethod = "post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "更新成功"),
            @ApiResponse(code = 400, message = "找不到页面"),
            @ApiResponse(code = 500, message = "更新失败")
    })
    @ResponseBody
    public ResponseData updateEmployeeBase(@RequestBody @ApiParam(name = "employeeId",value = "员工id",required = true) Employee employee){
        System.out.println("更新员工信息接口employee="+employee);
        int result = employeeBaseService.updateEmployeeBase(employee);
        if(result > 0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("更新成功");
            responseData.setData(employee.getEmployeeId());
        }else{
            responseData.setCode(Constants.NOTFOUND);
            responseData.setMsg("更新失败");
            responseData.setData(employee.getEmployeeId());
        }
        return responseData;
    }


    @PostMapping("/queryEmployeeBaseByEmployeeId")
    @ApiOperation(value = "查询员工信息接口", notes = "根据信息查询员工信息接口", httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "找不到页面"),
            @ApiResponse(code = 500, message = "查询失败")
    })
    @ResponseBody
    public ResponseData queryEmployeeBaseByEmployeeId(@RequestBody @ApiParam(name = "employeeId",value = "员工编码",required = true) Employee employee){
        Employee result = employeeBaseService.queryEmployeeBaseByEmployeeId(employee);
        if(result != null){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(result);
        }
        return responseData;
    }


    @GetMapping("/queryEmployeeBase")
    @ApiOperation(value = "查询员工信息接口", notes = "根据信息查询员工信息接口", httpMethod = "get", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "找不到页面"),
            @ApiResponse(code = 500, message = "查询失败")
    })
    @ResponseBody
    public ResponseData queryEmployeeBase(@RequestParam(defaultValue = "1") int currentPage, @RequestParam(defaultValue = "3")int pageSize){
        PaginationResult paginationResult = employeeBaseService.queryEmployeeBase(currentPage,pageSize);
        if(paginationResult != null){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(paginationResult);
        }else{
            responseData.setCode(Constants.NOTFOUND);
            responseData.setMsg("没有找到相关的信息");
        }
        return responseData;
    }

}
